package org.mihalovic.PES.GUI;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.plaf.ColorUIResource;

import edu.uci.ics.jung.visualization.GraphZoomScrollPane;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.control.ModalGraphMouse.Mode;
import org.apache.commons.collections15.Transformer;

import org.mihalovic.PES.Core.Graph.Edge;
import org.mihalovic.PES.Core.Graph.Node;
import org.mihalovic.PES.Core.Graph.Rank.PageRankScore;



/**
 * Simple panel to hold drawn graph
 */
public class SimpleGraphViewer extends JPanel {
    private double maxRank=0;

    public SimpleGraphViewer(final VisualizationViewer<Node, Edge> vv, double maxRank) {
        this.maxRank = maxRank==0 ? 1 : maxRank;

        setLayout(new GridLayout(1, 1));

        DefaultModalGraphMouse gm = new DefaultModalGraphMouse();
                
        gm.setMode(Mode.TRANSFORMING);
        vv.getRenderContext().setVertexFillPaintTransformer(vertexColor);
        vv.setGraphMouse(gm);
        vv.getRenderContext().setVertexShapeTransformer(vertexShape);
        vv.setVertexToolTipTransformer(vertexToolTip);

        UIManager.put("ToolTip.background", new ColorUIResource(255, 247, 200));    // The color is #fff7c8.
        UIManager.put("ToolTip.foreground", new ColorUIResource(76, 79, 83));
        Border border = BorderFactory.createLineBorder(new Color(76, 79, 83), 3);   // The color is #4c4f53.
        UIManager.put("ToolTip.border", border);
        ToolTipManager.sharedInstance().setDismissDelay(30000);                     // 30 seconds

        add(new GraphZoomScrollPane(vv));
    }


    Transformer<Node, Paint> vertexColor = new Transformer<Node, Paint>() {
        public Paint transform(Node vertex) {               //More rank, more red
            if(!vertex.HasInEdges())
                return new Color(230, 230, 240);
            
            float ff = 0;
            if(vertex.getRank()!=null)
                ff = (float)vertex.getRank().DoubleValue() / (float)maxRank;
            return new Color(1f, 1-ff, 1-ff, 1);
        }
    };

    Transformer<Node, Shape> vertexShape = new Transformer<Node, Shape>() {
        public Shape transform(Node vertex){
            float size = 18;
            
            if(!vertex.HasInEdges())
                size = 15;
            else if(vertex.getRank()!=null)
            {
                float ff = (float)vertex.getRank().DoubleValue() / (float)maxRank;
                size += ff*(1+(float)vertex.getRank().DoubleValue()) * 10;
            }
            return new Ellipse2D.Float(-(size/2f), -(size/2f), size, size);
        }
    };

    Transformer<Node, String> vertexToolTip = new Transformer<Node, String>() {
        public String transform(Node vertex){
            PageRankScore rank = vertex.getRank();
            String rankScore;
            if(rank != null)
                rankScore = rank.toString();
            else
                rankScore = "(not ranked)";
            return "<html><h3>Node: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+
                    vertex.getName() + " <br>Rank score:&nbsp;&nbsp;" + rankScore + "</h3></html>";
        }
    };

}
